package cn.fch.gotrip.service.impl;

import cn.fch.gotrip.dao.HotelDao;
import cn.fch.gotrip.dao.RoomDao;
import cn.fch.gotrip.entity.Hotel;
import cn.fch.gotrip.entity.HotelTheme;
import cn.fch.gotrip.entity.Room;
import cn.fch.gotrip.exception.NotFoundException;
import cn.fch.gotrip.exception.ValueException;
import cn.fch.gotrip.service.HotelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @program: gotrip
 * @description:
 * @CreatedBy: fch
 * @create: 2023-01-18 23:56
 **/
@Service
public class HotelServiceImpl implements HotelService {

    @Autowired
    private HotelDao hotelDao;

    @Autowired
    private RoomDao roomDao;

    @Override
    public boolean addHotel(Hotel hotel) {
        return hotelDao.addHotel(hotel) > 0;
    }

    @Override
    public boolean addHotelRoom(Room room) {
        if (null == room.getHotel().getId()) {
            throw new ValueException("房间不存在");
        }
        if (!hotelDao.isExistById(room.getHotel().getId())) {
            throw new ValueException("房间不存在");
        }
        return roomDao.addHotelRoom(room) > 0;
    }

    @Override
    public boolean updateHotel(Hotel hotel) {
        return hotelDao.updateHotel(hotel) > 0;
    }

    @Override
    public boolean updateRoom(Room room) {
        return roomDao.updateRoom(room) > 0;
    }

    @Override
    public Hotel updateHotelHtml(Integer code, Integer id, String html) {
        Hotel hotel = new Hotel();
        hotel.setId(id);
        switch (code) {
            case 1: {
                hotel.setIntroduce(html);
                break;
            }
            case 2: {
                hotel.setAddress(html);
                break;
            }
            case 3: {
                hotel.setRecommend(html);
                break;
            }
            case 4: {
                hotel.setSurrounding(html);
                break;
            }
        }
        if (hotelDao.updateHotelHtml(hotel) > 0) {
            return hotel;
        } else
            return null;
    }

    @Override
    public PageInfo<Hotel> queryAllHotelByThemeId(Integer themeId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Hotel> hotelList = hotelDao.queryAllHotelByThemeId(themeId);
        return new PageInfo<>(hotelList);
    }

    @Override
    public Hotel queryHotelById(Integer id) {
        Hotel hotel = hotelDao.queryHotelById(id);
        return Optional
                .ofNullable(hotel)
                .orElseThrow(() -> new NotFoundException("酒店不存在"));
    }

    @Override
    public List<HotelTheme> queryAllHotelTheme() {
        return hotelDao.queryAllHotelTheme();
    }

    @Override
    public Map<String, Object> queryBedAndBreakfast() {
        Map<String, Object> res = new HashMap<>(2);
        res.put("bed", roomDao.queryAllRoomBed());
        res.put("breakfast", roomDao.queryAllRoomBreakfast());
        return res;
    }

    @Override
    public List<Room> queryRoomByHotel(Integer id) {
        return roomDao.queryRoomByHotel(id);
    }
}
